<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class MemberController extends Controller
{
    //vip 页
    public function index()
    {

        //查询并分页
        $res = DB::table('user')->where('u_member','1')->paginate(6);
//
        //var_dump($res);die;

        if ($res) {

//            return view('admin.vip.index',['ress' => $res]);
            return view('admin.vip.index',['res' => $res]);


        }else{

            echo '失败';
        }
    }


    //ajax 拉黑
    public function ajaxblack($id,$dis)
    {
        $id = $id;

        $display = $dis == 1 ?'2':'1';

        $res = DB::update("update `user` set  `u_display` = $display where id = ?",[$id]);

        if ($res){

            return $this->index();

        }else{

            return $this->index();
        }
    }


    //用户详情页
    public function details($id)
    {

        $res = DB::table('userinfo')->where('i_id',$id)->get();

        $result = $res[0];

        //var_dump($result);

        return view('admin.vip.vipdetails',['res' => $result]);
    }


    //申请大V
    public function dav(Request $request)
    {

        //var_dump($request);

        //需要插入info 表的准备数据
        $arr = array(

            'i_id' => $request->i_id,
            'i_name' => $request->i_name,
            'i_adres' => $request->i_adres,
            'i_sex' => $request->i_sex,
            'i_birth' => $request->i_birth,
            'i_ox' => $request->i_ox,
            'i_boke' => $request->i_boke,
            'i_biref' => $request->i_biref,
            'i_occu' => $request->i_occu,
            'i_edu' => $request->i_edu,
            'i_tag' => $request->i_tag
        );

        //var_dump($arr);die;

        $result = DB::table('userinfo')->insert($arr);

        if($result){

            return redirect('/notice')->with(['message'=>'提交成功.管理员将会尽快为您审核','url' =>'succeslog', 'jumpTime'=>3,'status'=>true]);

        }

    }

    //查询申请大V  未审 的数据
    public function app()
    {

        $res = DB::table('userinfo')->where('v_app','1')->paginate(2);

        //var_dump($res);

        return view('admin.vip.vapplication',['res' => $res]);

    }


    //v 申请详细 信息 查询
    public  function vdetails($id)
    {
        $result = DB::table('userinfo')->where('id',$id)->get();

        //var_dump($result);

        if($result){
             $res = $result[0];
            return view('admin.vip.vdetails',['res' => $res]);
        }else{

            echo 'v 详细失败';
        }

    }


    //ajaxv  执行 同意 处理申请
    public function ajaxv($id,$userid)
    {
        //获取用户id
        $uid = $userid;

        $id = $id;
        $res = DB::update("update `userinfo` set  `v_app` = 2 where id = ?",[$id]);
        if($res){

            //修改user 表中的 v 字段
            $result = DB::update("update `user` set  `u_v` = 1 where id = ?",[$uid]);

            if($result){

                return redirect('/notice')->with(['message'=>'处理完成QAQ','url' =>'adm/vip/appv', 'jumpTime'=>3,'status'=>true]);

            }else{

                echo  '用户表修改失败';
            }


        }else{

           echo '失败!';
        }
    }


    //ajax 拒绝 v 申请 执行
    public function delv($id)
    {

        //echo $id;

        $res = DB::delete("delete from `userinfo` where `id` = ?",[$id]);

        if($res){
            return redirect('/notice')->with(['message'=>'处理完成QAQ','url' =>'adm/vip/appv', 'jumpTime'=>3,'status'=>true]);

        }else{

            echo '处理失败';
        }
    }
    
    
    
    //开通会员

    public function login_vip($id)
    {


        //查询当前是否已开通会员

        $re = DB::select('select `u_member`,`u_date` from `user` where id =?',[$id]);


        if ($re[0]->u_member == 1){

            echo '3';

        }else{


            //获取当天的时间
            $regtime= date('Y-m-d');

            //转为时间戳
            $chu = strtotime($regtime);

            //计算三十天有效时间戳
            $times = 31*24*60*60;

            //计算一个月之后的时间
            $t = time() + $times;

            //转为日期格式
            $b= date('Y-m-d',$t);

            //$c = $regtime.'---'.$b;

            $arr= array(
                'u_date' => $b,
                'u_member' => 1
            );

            $res = DB::table('user')->where('id',$id)->update($arr);

            if ($res){
                echo '1';
            }else{
                echo '2';
            }


        }





    }



}
